package LeetCode_20;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/10/26 15:38
 * 括弧匹配
 */
public class Solution {
    private boolean isMatch(char left,char right){
        if (left == '('){
            return right == ')';
        }
        if (left == '['){
            return right == ']';
        }
        if (left == '{'){
            return right == '}';
        }
        return false;
    }
    public boolean isValid(String s) {
        Deque<Character> stack = new LinkedList<>();
        char[] ch = s.toCharArray();
        for (char chars : ch ) {
            if (chars == '(' ||chars == '[' || chars == '{'){
                stack.push(chars);
            }else {
                if (stack.isEmpty()){
                    return false;
                }
                if (!isMatch(stack.pop(),chars)){
                    return false;
                }
            }
        }
        if(stack.isEmpty()){
            return true;
        }else {
            return false;
        }
    }
}
